rm(list = ls())
source("code/_preamble.R")

#________________________________
##FIGURE A3

#________________________________
##Load the dataset
load("data/attack_week.RData")

#________________________________
##Analysis
full <- feols(attacks ~ curfew_lead_seven + curfew_lead_six + curfew_lead_five + 
             curfew_lead_four + curfew_lead_three + curfew_lead_two + curfew_lead + 
             curfew + curfew_lag + curfew_lag_two + curfew_lag_three +
             curfew_lag_four + curfew_lag_five + curfew_lag_six + curfew_lag_seven, 
           data = elections[elections$to_keep == 1,], cluster = ~town)

rebels <- feols(rebels ~ curfew_lead_seven + curfew_lead_six + curfew_lead_five + 
             curfew_lead_four + curfew_lead_three + curfew_lead_two + curfew_lead + 
             curfew + curfew_lag + curfew_lag_two + curfew_lag_three +
             curfew_lag_four + curfew_lag_five + curfew_lag_six + curfew_lag_seven, 
             data = elections[elections$to_keep == 1,], cluster = ~town)

government <- feols(government ~ curfew_lead_seven + curfew_lead_six + curfew_lead_five + 
                   curfew_lead_four + curfew_lead_three + curfew_lead_two + curfew_lead + 
                   curfew + curfew_lag + curfew_lag_two + curfew_lag_three +
                   curfew_lag_four + curfew_lag_five + curfew_lag_six + curfew_lag_seven, 
                 data = elections[elections$to_keep == 1,], cluster = ~town)

#plotting
to_plot <- data.frame(coefs = c(full$coefficients[-1], rebels$coefficients[-1], government$coefficients[-1]),
                      ses = c(full$se[-1], rebels$se[-1], government$se[-1]),
                      type = c(rep("Attacks total", 15), rep("Attacks by rebels", 15), rep("Attacks by the government", 15)),
                      days = rep(1:15, 3))
to_plot$lower <- to_plot$coefs - to_plot$ses*1.96
to_plot$upper <- to_plot$coefs + to_plot$ses*1.96
to_plot$time <- c("t-7", "t-6", "t-5", "t-4", "t-3", "t-2", "t-1", "t", 
                  "t+1", "t+2", "t+3", "t+4", "t+5", "t+6", "t+7") 
ggplot(to_plot, aes(days, coefs)) + 
  geom_point() +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "darkblue") + 
  labs(x = "Time", y = expression(hat(beta)~" and 95% CI")) +
  theme_bw() +
  theme(axis.text.y =  element_text(size = 11), axis.text.x = element_text(size = 11), 
        axis.title.y =  element_text(size = 13), axis.title.x = element_text(size = 13),
        strip.text.x = element_text(size = 13),
        strip.background = element_rect(fill='grey', color='white')) +
  scale_x_continuous(breaks = 1:15, labels = to_plot$time[1:15]) + 
  facet_wrap(~type, ncol = 1)  


